﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Honestysoft.Financing.DataAccess;
using System.Data.Common;
using Honestysoft.Financing.Model;
using Honestysoft.BaseObject;

namespace Honestysoft.Financing.BusinessLogic
{
    public class Reports
    {
        string _connectionStringName;
        /// <summary>
        /// 构造函数，使用指定的连接字符串
        /// </summary>
        /// <param name="connectionStringName">连接字符串名称</param>
        public Reports(string connectionStringName)
        {
            _connectionStringName = connectionStringName;
        }
        /// <summary>
        /// 得到公司名称
        /// </summary>
        /// <param name="arrId"></param>
        /// <returns></returns>
        public DataTable  GetCompanyName(string[] arrId)
        {
            return new Reports_DAL().GetCompanyName(arrId);
        }
        /// <summary>
        /// 得到公司名称
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public string GetCompanyName(string id)
        {
            string[] arrId = new string[] { id };
            DataTable dt = GetCompanyName(arrId);
            if (dt != null && dt.Rows.Count == 1)
            {
                return dt.Rows[0]["CompName"].ToString();
            }
            else
            {
                return "";
            }
        }

        public DataTable GetCompanyBankAccount()
        {
            return new Reports_DAL().GetCompanyBankAccount();
        }

        /// <summary>
        /// 得到所有公司
        /// </summary>
        /// <returns></returns>
        public DataTable GetAllCompany()
        {
            return new Reports_DAL(_connectionStringName).GetAllCompany();
        }
        /// <summary>
        /// 得到最大的公司编码
        /// </summary>
        /// <returns></returns>
        public string GetMaxCompanyCode()
        {
            return new Reports_DAL(_connectionStringName).GetMaxCompanyCode();
        }

        /// <summary>
        ///  按月统计
        /// </summary>
        /// <returns></returns>
        public DataTable GetSummaryByMonth()
        {
            return new Reports_DAL(_connectionStringName).GetSummaryByMonth();
        }
         /// <summary>
        /// 根据日期区间得到审核统计信息
        /// </summary>
        /// <param name="startDate"></param>
        /// <param name="endDate"></param>
        /// <returns></returns>
        public DataTable GetSummaryByDate(DateTime startDate, DateTime endDate)
        {
            return new Reports_DAL(_connectionStringName).GetSummaryByDate(startDate, endDate);
        }
        public string GetCompanyIdByName(string compName, DbTransaction transaction)
        {
            return new Reports_DAL(_connectionStringName).GetCompanyIdByName(compName, transaction);
        }

        /// <summary>
        /// 得到周报
        /// </summary>
        /// <param name="year"></param>
        /// <param name="week"></param>
        /// <returns></returns>
        public Bu_WeekReport GetWeekReport(int year, int week)
        {
            Bu_WeekReport report = new Bu_WeekReport_DAL(_connectionStringName).GetWeekReport(year, week);
            if (report != null)            
            {
                QueryParameters query = new QueryParameters();
                query.Add("WeekReportID", report.ID, QueryParameters.OperatorType.Equal);
                OrderParameters order = new OrderParameters();
                order.Add("SortNo");
                report.Others = new Bu_WeekReport_Other_DAL(_connectionStringName).SelectEntitys(query, order);
            }
            return report;
        }
        /// <summary>
        /// 保存周报
        /// </summary>
        /// <param name="id"></param>
        /// <param name="startDate"></param>
        /// <param name="endDate"></param>
        /// <param name="sendDate"></param>
        /// <returns></returns>
        public bool SaveWeekReport(string id, DateTime startDate, DateTime endDate, DateTime sendDate)
        {
            return new Bu_WeekReport_DAL(_connectionStringName).SaveWeekReport(id, startDate, endDate, sendDate) == 1;
        }

        /// <summary>
        /// 创建并返回周报
        /// </summary>
        /// <param name="year"></param>
        /// <param name="week"></param>
        /// <param name="userId"></param>
        /// <param name="userName"></param>
        /// <returns></returns>
        public Bu_WeekReport CreateWeekReport(int year, int week, string userId,string userName)
        {
            Bu_WeekReport report = new Bu_WeekReport();
            report.CreateTime = DateTime.Now;
            report.Creator = userName;
            report.CreatorID = userId;
            Honestysoft.BaseLibrary.Common.DateHelper.DateRange range = Honestysoft.BaseLibrary.Common.DateHelper.GetWeekRange(year, week);
            report.StartDate = range.StartDate;
            report.EndDate = range.EndDate;
            report.ID = Honestysoft.BaseLibrary.Common.Guid.GetGuid();
            report.SendDate = DateTime.Today;
            report.TheYear = year;
            report.Week = week;
            new Bu_WeekReport_DAL(_connectionStringName).InsertEntity(report);
            return report;
        }
        /// <summary>
        /// 得到周报其他事项的下一个排序号
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int GetWeekReportOtherNextSortNo(string id)
        {
            return new Bu_WeekReport_Other_DAL(_connectionStringName).GetWeekReportOtherNextSortNo(id);
        }
        /// <summary>
        /// 得到项目信息及产值（已赴现场考察）
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public DataTable GetR010_1(DateTime date)
        {
            return new Reports_DAL(_connectionStringName).GetR010_1(date);
        }

        

        /// <summary>
        /// 得到部分项目工程建设计划进度示意图（已赴现场考察）
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public DataTable GetR010(DateTime date)
        {
            return new Reports_DAL(_connectionStringName).GetR010(date);
        }

        /// <summary>
        /// 得到项目投资计划完成情况及工程进度和资金拨付比例示意图
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public DataTable GetR011(DateTime date)
        {
            return new Reports_DAL(_connectionStringName).GetR011(date);
        }
        /// <summary>
        /// 保存银行利息
        /// </summary>
        /// <param name="lsBankRate"></param>
        /// <returns></returns>
        public bool SaveBankRate(List<Bu_BankRate> lsBankRate)
        {
            bool bRet = false;
            DbTransaction transaction = Honestysoft.BaseLibrary.Data.DatabaseProvider.GetDbTransaction(_connectionStringName);
            DbConnection connection = transaction.Connection;
            try
            {
                Bu_BankRate_DAL dal = new Bu_BankRate_DAL(_connectionStringName);
                dal.DeleteAll(transaction);
                dal.InsertEntitys(lsBankRate, transaction);
                transaction.Commit();
                bRet = true;
            }
            catch (Exception ex)
            {
                transaction.Rollback();
                Honestysoft.BaseLibrary.ExceptionHandling.ExceptionHelper.ExceptionDistribute(ex, false);
            }
            finally
            {
                connection.Close();
            }
            return bRet;
        }

        /// <summary>
        /// 保存银行利息
        /// </summary>
        /// <param name="lsIntDate"></param>
        /// <returns></returns>
        public bool SaveIntDate(List<Bu_InterestDate> lsIntDate)
        {
            bool bRet = false;
            DbTransaction transaction = Honestysoft.BaseLibrary.Data.DatabaseProvider.GetDbTransaction(_connectionStringName);
            DbConnection connection = transaction.Connection;
            try
            {
                Bu_InterestDate_DAL dal = new Bu_InterestDate_DAL(_connectionStringName);
                dal.DeleteAll(transaction);
                dal.InsertEntitys(lsIntDate, transaction);
                transaction.Commit();
                bRet = true;
            }
            catch (Exception ex)
            {
                transaction.Rollback();
                Honestysoft.BaseLibrary.ExceptionHandling.ExceptionHelper.ExceptionDistribute(ex, false);
            }
            finally
            {
                connection.Close();
            }
            return bRet;
        }
    }
}
